package com.hyc.service;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.google.common.collect.Lists;
import com.hyc.entity.User;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    private static List<User> users = null;
    static {
        User u1 = new User(1L, "张三", "zhangsan123", 20, "江苏");
        User u2 = new User(2L, "李四", "lisi123", 23, "浙江");
        User u3 = new User(3L, "王五", "wangwu123", 28, "安徽");
        users = Lists.newArrayList(u1, u2, u3);
    }

    public User findUserById(Long id){
        Optional<User> user = users.stream().filter(x -> x.getId().equals(id)).findFirst();
        if (user.isPresent()) {
            return user.get();
        }
        return null;
    }

    public User findByName(String name) {
        Optional<User> user = users.stream().filter(x -> x.getName().equals(name)).findFirst();
        if (user.isPresent()) {
            return user.get();
        }
        return null;
    }

    public static void main(String[] args) {
        UserService userService = new UserService();
        System.out.println(JSON.toJSONString(userService.findUserById(1L)));
    }
}
